﻿using System;
using System.Threading.Tasks;
using ERP.ERPSystem.HumanResources;
using ERP.Migrations;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Dtos;

namespace ERP.Web.Controllers.HumanResources
{
    /// <summary>
    /// 入职申请控制器
    /// </summary>
    [Route("[controller]/[action]")] // 定义控制器路由, 例如: /OnboardingApplication/ActionName
    public class OnboardingApplicationController : Controller
    {
        /// <summary>
        /// 入职申请仓储
        /// </summary>
        private readonly IOnboardingServices _onboardingServices;
        
        /// <summary>
        /// 构造函数，通过依赖注入获取入职申请服务
        /// </summary>
        /// <param name="onboardingServices">入职申请服务实例</param>
        public OnboardingApplicationController(IOnboardingServices onboardingServices)
        {
            this._onboardingServices = onboardingServices;
        }

        /// <summary>
        /// 返回入职申请的主视图页面
        /// </summary>
        /// <returns>ApplicationView.cshtml 视图</returns>
        [HttpGet]
        public IActionResult ApplicationView()
        {
            return View();
        }

        /// <summary>
        /// (API) 分页查询入职申请数据
        /// </summary>
        /// <param name="PersonName">(筛选条件) 申请人姓名</param>
        /// <param name="InterviewPeople">(筛选条件) 面试官姓名</param>
        /// <param name="page">页码，从1开始</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns>包含分页数据的OkObjectResult</returns>
        [HttpGet]
        public async Task<IActionResult> QueryPaged(string? PersonName, string? InterviewPeople, int page = 1, int pageSize = 10)
        {
            var result = await _onboardingServices.QueryPaged(PersonName, InterviewPeople, page, pageSize);
            return Ok(result);
        }

        /// <summary>
        /// (API) 根据筛选条件导出数据到Excel文件
        /// </summary>
        /// <param name="PersonName">(筛选条件) 申请人姓名</param>
        /// <param name="InterviewPeople">(筛选条件) 面试官姓名</param>
        /// <returns>一个可供浏览器下载的Excel文件</returns>
        [HttpGet]
        public async Task<IActionResult> Export(string? PersonName, string? InterviewPeople)
        {
            // 1. 调用服务层方法获取Excel文件的字节数组
            var fileBytes = await _onboardingServices.ExportToExcel(PersonName, InterviewPeople);
            // 2. 定义下载的文件名
            var fileName = $"入职申请数据_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
            
            // 3. 返回FileResult，触发浏览器下载
            return File(
                fileContents: fileBytes, // 文件内容
                contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // MIME类型
                fileDownloadName: fileName // 下载时显示的文件名
            );
        }

    }
}
